﻿<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
	xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib" xmlns:TB4="clr-namespace:TB4"
	xmlns:Converters="clr-namespace:WPF.Common.Converters;assembly=WPF.Common" x:Class="TB4.Window1"
	x:Name="Window"
	Title="Window1"
	Width="640" Height="480">

    <Window.Resources>

        <Converters:ImageSourceToImageConverter x:Key="ImageSourceToImageConverter" />
        <Converters:IsGreaterThanConverter x:Key="GreaterThanConverter" />

        <Converters:VisibilityConverter x:Key="VisibilityConverter" />
        <Style x:Key="ToolbarItemContainerStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected, Converter={StaticResource VisibilityConverter}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

        <Style x:Key="GroupToolbarGroupStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <ListBox 
    						Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type TB4:TheRealGroupToolBar}, Mode=FindAncestor}}"
    						ItemsSource="{Binding Items}" 
    						ItemTemplate="{Binding ToolbarItemTemplate, RelativeSource={RelativeSource AncestorType={x:Type TB4:TheRealGroupToolBar}, Mode=FindAncestor}}"
    						ItemContainerStyle="{StaticResource ToolbarItemContainerStyle}"
                            SelectedIndex="0"
    						ContextMenuService.Placement="Right" ContextMenuService.IsEnabled="{Binding Items.Count, Converter={StaticResource GreaterThanConverter}, ConverterParameter=1}">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ContextMenu>
                                <TB4:SelectorContextMenu 
                                    HorizontalContentAlignment="Stretch"
    								ItemsSource="{Binding PlacementTarget.Items, RelativeSource={RelativeSource Self}}"
    								ItemContainerStyle="{Binding PlacementTarget.Tag.SelectorContextMenuItemContainerStyle, RelativeSource={RelativeSource Self}}"
    								ItemTemplate="{Binding PlacementTarget.Tag.SelectorContextMenuItemTemplate, RelativeSource={RelativeSource Self}}" />
                            </ListBox.ContextMenu>
                            <ListBox.Template>
                                <ControlTemplate>
                                    <Grid>
                                        <ItemsPresenter />
                                        <Viewbox Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(ContextMenuService.IsEnabled), Converter={StaticResource VisibilityConverter}}" VerticalAlignment="Bottom" HorizontalAlignment="Right" MaxHeight="4" MaxWidth="4" Stretch="Uniform" Margin="0,0,1,1" >
                                            <Path Data="M103.37506,32.875 L122.688,15.666622 122.5625,32.875 z"  Stretch="Fill" >
                                                <Path.Fill>
                                                    <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
                                                </Path.Fill>
                                            </Path>
                                        </Viewbox>
                                    </Grid>
                                </ControlTemplate>
                            </ListBox.Template>
                        </ListBox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Collections:ArrayList x:Key="Tools">
            <TB4:Tool Name="Select" ImageSource="Images\Mouse_pointer_or_cursor.png" Category="Category 1" />
            <TB4:Tool Name="Bucket" ImageSource="Images\Painting.png" Category="Category 1" />
            <TB4:Tool Name="Pen" ImageSource="Images\Pen.png" Category="Category 1" />
            <TB4:Tool Name="Move" ImageSource="Images\hand.png" Category="Category 2"/>
            <TB4:Tool Name="Brush" ImageSource="Images\brush.png" Category="Category 2" />
            <TB4:Tool Name="Chicken Tool" ImageSource="Images\splash-chick.png" Category="Category 3" />
        </Collections:ArrayList>
        <CollectionViewSource x:Key="CollectionViewSource" Source="{StaticResource Tools}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Category"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>



        <SolidColorBrush x:Key="ToolBarHorizontalBackground" Color="#FFEEF5FD"/>
        <SolidColorBrush x:Key="ToolBarToggleButtonVerticalBackground" Color="#FFEEF5FD"/>
        <SolidColorBrush x:Key="ToolBarButtonHover" Color="#210080FF"/>
        <SolidColorBrush x:Key="ToolBarGripper" Color="#FF6D6D6D"/>
        <Style x:Key="ToolBarVerticalOverflowButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Background" Value="{StaticResource ToolBarToggleButtonVerticalBackground}"/>
            <Setter Property="MinHeight" Value="0"/>
            <Setter Property="MinWidth" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border x:Name="Bd" Background="{TemplateBinding Background}" CornerRadius="0,0,3,3" SnapsToDevicePixels="true">
                            <Canvas HorizontalAlignment="Right" Height="6" Margin="2,7,2,2" SnapsToDevicePixels="true" VerticalAlignment="Bottom" Width="7">
                                <Path Data="M 1.5 1 L 1.5 6" Stroke="White"/>
                                <Path Data="M 0.5 0 L 0.5 5" Stroke="{TemplateBinding Foreground}"/>
                                <Path Data="M 3.5 0.5 L 7 3.5 L 4 6.5 Z" Fill="White"/>
                                <Path Data="M 3 -0.5 L 6 2.5 L 3 5.5 Z" Fill="{TemplateBinding Foreground}"/>
                            </Canvas>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ToolBarButtonHover}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ToolBarButtonHover}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{StaticResource ToolBarGripper}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="true">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <SolidColorBrush x:Key="ToolBarVerticalBackground" Color="#FFEEF5FD"/>
        <SolidColorBrush x:Key="ToolBarToggleButtonHorizontalBackground" Color="#FFEEF5FD"/>
        <Style x:Key="ToolBarHorizontalOverflowButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Background" Value="{StaticResource ToolBarToggleButtonHorizontalBackground}"/>
            <Setter Property="MinHeight" Value="0"/>
            <Setter Property="MinWidth" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border x:Name="Bd" Background="{TemplateBinding Background}" CornerRadius="0,3,3,0" SnapsToDevicePixels="true">
                            <Canvas HorizontalAlignment="Right" Height="7" Margin="7,2,2,2" SnapsToDevicePixels="true" VerticalAlignment="Bottom" Width="6">
                                <Path Data="M 1 1.5 L 6 1.5" Stroke="White"/>
                                <Path Data="M 0 0.5 L 5 0.5" Stroke="{TemplateBinding Foreground}"/>
                                <Path Data="M 0.5 4 L 6.5 4 L 3.5 7 Z" Fill="White"/>
                                <Path Data="M -0.5 3 L 5.5 3 L 2.5 6 Z" Fill="{TemplateBinding Foreground}"/>
                            </Canvas>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ToolBarButtonHover}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource ToolBarButtonHover}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{StaticResource ToolBarGripper}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="true">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <SolidColorBrush x:Key="ToolBarMenuBorder" Color="#FFB6BDC5"/>
        <SolidColorBrush x:Key="ToolBarSubMenuBackground" Color="#FFEEF5FD"/>
        <Style x:Key="ToolBarThumbStyle" TargetType="{x:Type Thumb}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Background="Transparent" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                            <Rectangle>
                                <Rectangle.Fill>
                                    <DrawingBrush TileMode="Tile" Viewbox="0,0,4,4" Viewport="0,0,4,4" ViewportUnits="Absolute" ViewboxUnits="Absolute">
                                        <DrawingBrush.Drawing>
                                            <DrawingGroup>
                                                <GeometryDrawing Brush="White" Geometry="M 1 1 L 1 3 L 3 3 L 3 1 z"/>
                                                <GeometryDrawing Brush="{StaticResource ToolBarGripper}" Geometry="M 0 0 L 0 2 L 2 2 L 2 0 z"/>
                                            </DrawingGroup>
                                        </DrawingBrush.Drawing>
                                    </DrawingBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Cursor" Value="SizeAll"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ToolBarMainPanelBorderStyle" TargetType="{x:Type Border}">
            <Setter Property="Margin" Value="0,0,11,0"/>
            <Setter Property="CornerRadius" Value="3,3,3,3"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="true">
                    <Setter Property="CornerRadius" Value="0,0,0,0"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        
        
        <Style TargetType="{x:Type TB4:TheRealGroupToolBar}">
            <Setter Property="DefaultGroupStyle">
                <Setter.Value>
                    <GroupStyle ContainerStyle="{StaticResource GroupToolbarGroupStyle}" />
                </Setter.Value>
            </Setter>
            <Setter Property="ItemTemplate" Value="{Binding RelativeSource={RelativeSource Self}, Path=NoGroupsItemTemplate}" />
            <Setter Property="Background" Value="{StaticResource ToolBarHorizontalBackground}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToolBar}">
                        <Grid x:Name="Grid" Margin="3,1,1,1" SnapsToDevicePixels="true">
                            <Grid x:Name="OverflowGrid" HorizontalAlignment="Right">
                                <ToggleButton x:Name="OverflowButton" ClickMode="Press" FocusVisualStyle="{x:Null}" IsChecked="{Binding IsOverflowOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" IsEnabled="{TemplateBinding HasOverflowItems}" Style="{StaticResource ToolBarHorizontalOverflowButtonStyle}"/>
                                <Popup x:Name="OverflowPopup" AllowsTransparency="true" Focusable="false" IsOpen="{Binding IsOverflowOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom" StaysOpen="false">
                                    <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent">
                                        <Border x:Name="ToolBarSubMenuBorder" BorderBrush="{StaticResource ToolBarMenuBorder}" BorderThickness="1" Background="{StaticResource ToolBarSubMenuBackground}" RenderOptions.ClearTypeHint="Enabled">
                                            <ToolBarOverflowPanel x:Name="PART_ToolBarOverflowPanel" KeyboardNavigation.DirectionalNavigation="Cycle" FocusVisualStyle="{x:Null}" Focusable="true" Margin="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle" WrapWidth="200"/>
                                        </Border>
                                    </Themes:SystemDropShadowChrome>
                                </Popup>
                            </Grid>
                            <Border x:Name="MainPanelBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" Style="{StaticResource ToolBarMainPanelBorderStyle}">
                                <DockPanel KeyboardNavigation.TabIndex="1" KeyboardNavigation.TabNavigation="Local">
                                    <Thumb x:Name="ToolBarThumb" Margin="-3,-1,0,0" Padding="6,5,1,6" Style="{StaticResource ToolBarThumbStyle}" Width="10"/>
                                    <ContentPresenter x:Name="ToolBarHeader" ContentSource="Header" HorizontalAlignment="Center" Margin="4,0,4,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                                    <ToolBarPanel x:Name="PART_ToolBarPanel" IsItemsHost="True" Margin="0,1,2,2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </DockPanel>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsOverflowOpen" Value="true">
                                <Setter Property="IsEnabled" TargetName="ToolBarThumb" Value="false"/>
                            </Trigger>
                            <Trigger Property="Header" Value="{x:Null}">
                                <Setter Property="Visibility" TargetName="ToolBarHeader" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="ToolBarTray.IsLocked" Value="true">
                                <Setter Property="Visibility" TargetName="ToolBarThumb" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasDropShadow" SourceName="OverflowPopup" Value="true">
                                <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                                <Setter Property="SnapsToDevicePixels" TargetName="Shdw" Value="true"/>
                                <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
                            </Trigger>
                            <Trigger Property="Orientation" Value="Vertical">
                                <Setter Property="Margin" TargetName="Grid" Value="1,3,1,1"/>
                                <Setter Property="Style" TargetName="OverflowButton" Value="{StaticResource ToolBarVerticalOverflowButtonStyle}"/>
                                <Setter Property="Height" TargetName="ToolBarThumb" Value="10"/>
                                <Setter Property="Width" TargetName="ToolBarThumb" Value="Auto"/>
                                <Setter Property="Margin" TargetName="ToolBarThumb" Value="-1,-3,0,0"/>
                                <Setter Property="Padding" TargetName="ToolBarThumb" Value="5,6,6,1"/>
                                <Setter Property="Margin" TargetName="ToolBarHeader" Value="0,0,0,4"/>
                                <!--<Setter Property="Margin" TargetName="PART_ToolBarPanel" Value="1,0,2,2"/>-->
                                <Setter Property="DockPanel.Dock" TargetName="ToolBarThumb" Value="Top"/>
                                <Setter Property="DockPanel.Dock" TargetName="ToolBarHeader" Value="Top"/>
                                <Setter Property="HorizontalAlignment" TargetName="OverflowGrid" Value="Stretch"/>
                                <Setter Property="VerticalAlignment" TargetName="OverflowGrid" Value="Bottom"/>
                                <Setter Property="Placement" TargetName="OverflowPopup" Value="Right"/>
                                <Setter Property="Margin" TargetName="MainPanelBorder" Value="0,0,0,11"/>
                                <Setter Property="Background" Value="{StaticResource ToolBarVerticalBackground}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={x:Static SystemParameters.HighContrast}}" Value="true">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        <ToolBarTray HorizontalAlignment="Left" Height="439" Margin="10,0,0,0" VerticalAlignment="Top"  Orientation="Vertical">
            <ToolBar >
                <Rectangle Fill="#FF1F1F64" Stroke="Black" Height="32" Width="30"/>
                <Rectangle Fill="#FF7979B8" Stroke="Black" Height="32" Width="30"/>
            </ToolBar>
            <TB4:TheRealGroupToolBar x:Name="theRealGroupToolBar" ItemsSource="{Binding Source={StaticResource CollectionViewSource}}">
                <TB4:TheRealGroupToolBar.ToolbarItemTemplate>
                    <DataTemplate DataType="TB4:Tool">
                        <DockPanel>
                            <TextBlock DockPanel.Dock="Bottom" Text="{Binding Name}" />
                            <Image Width="30" Height="30" Source="{Binding ImageSource}" />
                        </DockPanel>
                    </DataTemplate>
                </TB4:TheRealGroupToolBar.ToolbarItemTemplate>
                <TB4:TheRealGroupToolBar.SelectorContextMenuItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </TB4:TheRealGroupToolBar.SelectorContextMenuItemTemplate>

                <TB4:TheRealGroupToolBar.SelectorContextMenuItemContainerStyle>
                    <Style TargetType="TB4:SelectorContextMenuItem">
                        <Setter Property="Icon" Value="{Binding ImageSource, Converter={StaticResource ImageSourceToImageConverter}}" />
                    </Style>
                </TB4:TheRealGroupToolBar.SelectorContextMenuItemContainerStyle>
            </TB4:TheRealGroupToolBar>
            <ToolBar >
                <Rectangle Fill="#FF1F1F64" Stroke="Black" Height="32" Width="30"/>
                <Rectangle Fill="#FF7979B8" Stroke="Black" Height="32" Width="30"/>
            </ToolBar>
        </ToolBarTray>
        <ListBox HorizontalAlignment="Left" Height="237" Margin="205,73,0,0" VerticalAlignment="Top" Width="201" SelectedItem="{Binding ElementName=theRealGroupToolBar, Path=SelectedItem}" ItemsSource="{Binding Mode=OneWay, Source={StaticResource CollectionViewSource}}"/>
        <TextBlock HorizontalAlignment="Left" Margin="205,366,0,0" TextWrapping="Wrap" Text="{Binding SelectedItem, ElementName=theRealGroupToolBar}" VerticalAlignment="Top" Width="171"/>
    </Grid>
</Window>